home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / frasrc19.zip / JB.C < prev    next >
C/C++ Source or Header  |  1995-01-29  |  11KB  |  445 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "fractint.h"
  4. #include "mpmath.h"
  5. #include "helpdefs.h"
  6. #include "prototyp.h"
  7. #include "fractype.h"
  8.  
  9. /* these need to be accessed elsewhere for saving data */
  10. double mxminfp = -.83;
  11. double myminfp = -.25;
  12. double mxmaxfp = -.83;
  13. double mymaxfp =  .25;
  14.  
  15. static long mxmin, mymin;
  16. static long x_per_inch, y_per_inch, inch_per_xdot, inch_per_ydot;
  17. static double x_per_inchfp, y_per_inchfp, inch_per_xdotfp, inch_per_ydotfp;
  18. static int bbase;
  19. static long xpixel, ypixel;
  20. static double xpixelfp, ypixelfp;
  21. static long initz, djx, djy, dmx, dmy;
  22. static double initzfp, djxfp, djyfp, dmxfp, dmyfp;
  23. static long jx, jy, mx, my, xoffset, yoffset;
  24. static double jxfp, jyfp, mxfp, myfp, xoffsetfp, yoffsetfp;
  25.  
  26. struct Perspective
  27. {
  28.    long x, y, zx, zy;
  29. };
  30.  
  31. struct Perspectivefp
  32. {
  33.    double x, y, zx, zy;
  34. };
  35.  
  36. struct Perspective LeftEye, RightEye, *Per;
  37. struct Perspectivefp LeftEyefp, RightEyefp, *Perfp;
  38.  
  39. _LCMPLX jbc;
  40. _CMPLX jbcfp;
  41.  
  42. static double fg, fg16;
  43. int zdots = 128;
  44.  
  45. float originfp  = (float)8.0;
  46. float heightfp  = (float)7.0;
  47. float widthfp   = (float)10.0;
  48. float distfp    = (float)24.0;
  49. float eyesfp    = (float)2.5;
  50. float depthfp   = (float)8.0;
  51. float brratiofp = (float)1.0;
  52. static long width, dist, eyes, depth, brratio;
  53.  
  54. int juli3Dmode = 0;
  55.  
  56. int neworbittype = JULIA;
  57.  
  58. int
  59. JulibrotSetup(void)
  60. {
  61.    long origin;
  62.    int r = 0;
  63.    char *mapname;
  64.  
  65. #ifndef XFRACT
  66.    if (colors < 255)
  67.    {
  68.       static FCODE msg[] =
  69.       {"Sorry, but Julibrots require a 256-color video mode"};
  70.       stopmsg(0, msg);
  71.       return (0);
  72.    }
  73. #endif
  74.  
  75.    xoffsetfp = (xxmax + xxmin) / 2;     /* Calculate average */
  76.    yoffsetfp = (yymax + yymin) / 2;     /* Calculate average */
  77.    dmxfp = (mxmaxfp - mxminfp) / zdots;
  78.    dmyfp = (mymaxfp - myminfp) / zdots;
  79.    floatparm = &jbcfp;
  80.    x_per_inchfp = (xxmin - xxmax) / widthfp;
  81.    y_per_inchfp = (yymax - yymin) / heightfp;
  82.    inch_per_xdotfp = widthfp / xdots;
  83.    inch_per_ydotfp = heightfp / ydots;
  84.    initzfp = originfp - (depthfp / 2);
  85.    if(juli3Dmode == 0)
  86.       RightEyefp.x = 0.0;
  87.    else
  88.       RightEyefp.x = eyesfp / 2;
  89.    LeftEyefp.x = -RightEyefp.x;
  90.    LeftEyefp.y = RightEyefp.y = 0;
  91.    LeftEyefp.zx = RightEyefp.zx = distfp;
  92.    LeftEyefp.zy = RightEyefp.zy = distfp;
  93.    bbase = 128;
  94.  
  95. #ifndef XFRACT
  96.    if (fractalspecific[fractype].isinteger > 0)
  97.    {
  98.       long jxmin, jxmax, jymin, jymax, mxmax, mymax;
  99.       if (fractalspecific[neworbittype].isinteger == 0)
  100.       {
  101.          static FCODE msg[] = {"Julibrot orbit type isinteger mismatch"};
  102.          stopmsg(0, (char far *)msg);
  103.       }
  104.       if (fractalspecific[neworbittype].isinteger > 1)
  105.          bitshift = fractalspecific[neworbittype].isinteger;
  106.       fg = (double) (1L << bitshift);
  107.       fg16 = (double) (1L << 16);
  108.       jxmin = (long) (xxmin * fg);
  109.       jxmax = (long) (xxmax * fg);
  110.       xoffset = (jxmax + jxmin) / 2;    /* Calculate average */
  111.       jymin = (long) (yymin * fg);
  112.       jymax = (long) (yymax * fg);
  113.       yoffset = (jymax + jymin) / 2;    /* Calculate average */
  114.       mxmin = (long) (mxminfp * fg);
  115.       mxmax = (long) (mxmaxfp * fg);
  116.       mymin = (long) (myminfp * fg);
  117.       mymax = (long) (mymaxfp * fg);
  118.       origin = (long) (originfp * fg16);
  119.       depth = (long) (depthfp * fg16);
  120.       width = (long) (widthfp * fg16);
  121.       dist = (long) (distfp * fg16);
  122.       eyes = (long) (eyesfp * fg16);
  123.       brratio = (long) (brratiofp * fg16);
  124.       dmx = (mxmax - mxmin) / zdots;
  125.       dmy = (mymax - mymin) / zdots;
  126.       longparm = &jbc;
  127.  
  128.       x_per_inch = (long) ((xxmin - xxmax) / widthfp * fg);
  129.       y_per_inch = (long) ((yymax - yymin) / heightfp * fg);
  130.       inch_per_xdot = (long) ((widthfp / xdots) * fg16);
  131.       inch_per_ydot = (long) ((heightfp / ydots) * fg16);
  132.       initz = origin - (depth / 2);
  133.       if(juli3Dmode == 0)
  134.          RightEye.x = 0l;
  135.       else
  136.          RightEye.x = eyes / 2;
  137.       LeftEye.x = -RightEye.x;
  138.       LeftEye.y = RightEye.y = 0l;
  139.       LeftEye.zx = RightEye.zx = dist;
  140.       LeftEye.zy = RightEye.zy = dist;
  141.       bbase = (int) (128.0 * brratiofp);
  142.    }
  143. #endif
  144.  
  145.    if (juli3Dmode == 3)
  146.    {
  147.       savedac = 0;
  148.       mapname = Glasses1Map;
  149.    }
  150.    else
  151.       mapname = GreyFile;
  152.    if(savedac != 1)
  153.    {
  154.    if (ValidateLuts(mapname) != 0)
  155.       return (0);
  156.    spindac(0, 1);               /* load it, but don't spin */
  157.       if(savedac == 2)
  158.         savedac = 1;
  159.    }
  160.    return (r >= 0);
  161. }
  162.  
  163.  
  164. int
  165. jb_per_pixel(void)
  166. {
  167.    jx = multiply(Per->x - xpixel, initz, 16);
  168.    jx = divide(jx, dist, 16) - xpixel;
  169.    jx = multiply(jx << (bitshift - 16), x_per_inch, bitshift);
  170.    jx += xoffset;
  171.    djx = divide(depth, dist, 16);
  172.    djx = multiply(djx, Per->x - xpixel, 16) << (bitshift - 16);
  173.    djx = multiply(djx, x_per_inch, bitshift) / zdots;
  174.  
  175.    jy = multiply(Per->y - ypixel, initz, 16);
  176.    jy = divide(jy, dist, 16) - ypixel;
  177.    jy = multiply(jy << (bitshift - 16), y_per_inch, bitshift);
  178.    jy += yoffset;
  179.    djy = divide(depth, dist, 16);
  180.    djy = multiply(djy, Per->y - ypixel, 16) << (bitshift - 16);
  181.    djy = multiply(djy, y_per_inch, bitshift) / zdots;
  182.  
  183.    return (1);
  184. }
  185.  
  186. int
  187. jbfp_per_pixel(void)
  188. {
  189.    jxfp = ((Perfp->x - xpixelfp) * initzfp / distfp - xpixelfp) * x_per_inchfp;
  190.    jxfp += xoffsetfp;
  191.    djxfp = (depthfp / distfp) * (Perfp->x - xpixelfp) * x_per_inchfp / zdots;
  192.  
  193.    jyfp = ((Perfp->y - ypixelfp) * initzfp / distfp - ypixelfp) * y_per_inchfp;
  194.    jyfp += yoffsetfp;
  195.    djyfp = depthfp / distfp * (Perfp->y - ypixelfp) * y_per_inchfp / zdots;
  196.  
  197.    return (1);
  198. }
  199.  
  200. static int zpixel, plotted;
  201. static long n;
  202.  
  203. int
  204. zline(long x, long y)
  205. {
  206.    xpixel = x;
  207.    ypixel = y;
  208.    mx = mxmin;
  209.    my = mymin;
  210.    switch(juli3Dmode)
  211.    {
  212.    case 0:
  213.    case 1:
  214.       Per = &LeftEye;
  215.       break;
  216.    case 2:
  217.       Per = &RightEye;
  218.       break;
  219.    case 3:
  220.       if ((row + col) & 1)
  221.          Per = &LeftEye;
  222.       else
  223.          Per = &RightEye;
  224.       break;
  225.    }
  226.    jb_per_pixel();
  227.    for (zpixel = 0; zpixel < zdots; zpixel++)
  228.    {
  229.       lold.x = jx;
  230.       lold.y = jy;
  231.       jbc.x = mx;
  232.       jbc.y = my;
  233.       if (keypressed())
  234.          return (-1);
  235.       ltempsqrx = multiply(lold.x, lold.x, bitshift);
  236.       ltempsqry = multiply(lold.y, lold.y, bitshift);
  237.       for (n = 0; n < maxit; n++)
  238.          if (fractalspecific[neworbittype].orbitcalc())
  239.             break;
  240.       if (n == maxit)
  241.       {
  242.          if (juli3Dmode==3)
  243.          {
  244.             color = (int) (128l * zpixel / zdots);
  245.             if ((row + col) & 1)
  246.             {
  247.  
  248.                (*plot) (col, row, 127 - color);
  249.             }
  250.             else
  251.             {
  252.                color = (int) (multiply((long) color << 16, brratio, 16) >> 16);
  253.                if (color < 1)
  254.                   color = 1;
  255.                if (color > 127)
  256.                   color = 127;
  257.                (*plot) (col, row, 127 + bbase - color);
  258.             }
  259.          }
  260.          else
  261.          {
  262.             color = (int) (254l * zpixel / zdots);
  263.             (*plot) (col, row, color + 1);
  264.          }
  265.          plotted = 1;
  266.          break;
  267.       }
  268.       mx += dmx;
  269.       my += dmy;
  270.       jx += djx;
  271.       jy += djy;
  272.    }
  273.    return (0);
  274. }
  275.  
  276. int
  277. zlinefp(double x, double y)
  278. {
  279. #ifdef XFRACT
  280.    static int keychk = 0;
  281. #endif   
  282.    xpixelfp = x;
  283.    ypixelfp = y;
  284.    mxfp = mxminfp;
  285.    myfp = myminfp;
  286.    switch(juli3Dmode)
  287.    {
  288.    case 0:
  289.    case 1:
  290.       Perfp = &LeftEyefp;
  291.       break;
  292.    case 2:
  293.       Perfp = &RightEyefp;
  294.       break;
  295.    case 3:
  296.       if ((row + col) & 1)
  297.          Perfp = &LeftEyefp;
  298.       else
  299.          Perfp = &RightEyefp;
  300.       break;
  301.    }
  302.    jbfp_per_pixel();
  303.    for (zpixel = 0; zpixel < zdots; zpixel++)
  304.    {
  305.       old.x = jxfp;
  306.       old.y = jyfp;
  307.       jbcfp.x = mxfp;
  308.       jbcfp.y = myfp;
  309.       qc = param[0];
  310.       qci = param[1];
  311.       qcj = param[2];
  312.       qck = param[3];
  313. #ifdef XFRACT      
  314.       if (keychk++ > 500)
  315.       {
  316.          keychk = 0;
  317.          if (keypressed())
  318.             return (-1);
  319.       }
  320. #else
  321.       if (keypressed())
  322.          return (-1);
  323. #endif      
  324.       tempsqrx = sqr(old.x);
  325.       tempsqry = sqr(old.y);
  326.  
  327.       for (n = 0; n < maxit; n++)
  328.          if (fractalspecific[neworbittype].orbitcalc())
  329.             break;
  330.       if (n == maxit)
  331.       {
  332.          if (juli3Dmode == 3)
  333.          {
  334.             color = (int) (128l * zpixel / zdots);
  335.             if ((row + col) & 1)
  336.                (*plot) (col, row, 127 - color);
  337.             else
  338.             {
  339.                color = (int)(color * brratiofp);
  340.                if (color < 1)
  341.                   color = 1;
  342.                if (color > 127)
  343.                   color = 127;
  344.                (*plot) (col, row, 127 + bbase - color);
  345.             }
  346.          }
  347.          else
  348.          {
  349.             color = (int) (254l * zpixel / zdots);
  350.             (*plot) (col, row, color + 1);
  351.          }
  352.          plotted = 1;
  353.          break;
  354.       }
  355.       mxfp += dmxfp;
  356.       myfp += dmyfp;
  357.       jxfp += djxfp;
  358.       jyfp += djyfp;
  359.    }
  360.    return (0);
  361. }
  362.  
  363. int
  364. Std4dFractal(void)
  365. {
  366.    long x, y;
  367.    int xdot, ydot;
  368.    c_exp = (int)param[2];
  369.    if(neworbittype == LJULIAZPOWER)
  370.    {
  371.       if(c_exp < 1)
  372.          c_exp = 1;
  373.       if(param[3] == 0.0 && debugflag != 6000 && (double)c_exp == param[2])
  374.           fractalspecific[neworbittype].orbitcalc = longZpowerFractal;
  375.       else
  376.           fractalspecific[neworbittype].orbitcalc = longCmplxZpowerFractal;
  377.    }
  378.  
  379.    for (y = 0, ydot = (ydots >> 1) - 1; ydot >= 0; ydot--, y -= inch_per_ydot)
  380.    {
  381.       plotted = 0;
  382.       x = -(width >> 1);
  383.       for (xdot = 0; xdot < xdots; xdot++, x += inch_per_xdot)
  384.       {
  385.          col = xdot;
  386.          row = ydot;
  387.          if (zline(x, y) < 0)
  388.             return (-1);
  389.          col = xdots - col - 1;
  390.          row = ydots - row - 1;
  391.          if (zline(-x, -y) < 0)
  392.             return (-1);
  393.       }
  394.       if (plotted == 0)
  395.       {
  396.          if (y == 0)
  397.            plotted = -1;  /* no points first pass; don't give up */
  398.          else  
  399.            break;
  400.       }     
  401.    }
  402.    return (0);
  403. }
  404. int
  405. Std4dfpFractal(void)
  406. {
  407.    double x, y;
  408.    int xdot, ydot;
  409.    c_exp = (int)param[2];
  410.  
  411.    if(neworbittype == FPJULIAZPOWER)
  412.    {
  413.       if(param[3] == 0.0 && debugflag != 6000 && (double)c_exp == param[2])
  414.           fractalspecific[neworbittype].orbitcalc = floatZpowerFractal;
  415.       else
  416.           fractalspecific[neworbittype].orbitcalc = floatCmplxZpowerFractal;
  417.       get_julia_attractor (param[0], param[1]);    /* another attractor? */
  418.    }
  419.  
  420.    for (y = 0, ydot = (ydots >> 1) - 1; ydot >= 0; ydot--, y -= inch_per_ydotfp)
  421.    {
  422.       plotted = 0;
  423.       x = -widthfp / 2;
  424.       for (xdot = 0; xdot < xdots; xdot++, x += inch_per_xdotfp)
  425.       {
  426.          col = xdot;
  427.          row = ydot;
  428.          if (zlinefp(x, y) < 0)
  429.             return (-1);
  430.          col = xdots - col - 1;
  431.          row = ydots - row - 1;
  432.          if (zlinefp(-x, -y) < 0)
  433.             return (-1);
  434.       }
  435.       if (plotted == 0)
  436.       {
  437.          if (y == 0)
  438.            plotted = -1;  /* no points first pass; don't give up */
  439.          else  
  440.            break;
  441.       }     
  442.    }
  443.    return (0);
  444. }
  445.